/**
 * @(#)UlynBrower
 * 版权声明 厦门畅享信息技术有限公司, 版权所有 违者必究
 *
 *<br> Copyright:  Copyright (c) 2014
 *<br> Company:厦门畅享信息技术有限公司
 *<br> @author ulyn
 *<br> 14-8-14 下午11:40
 *<br> @version 1.0
 *————————————————————————————————
 *修改记录
 *    修改者：
 *    修改时间：
 *    修改原因：
 *————————————————————————————————
 */
package cn.ulyn.seckill.mockclient;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.IOException;

/**
 * <pre></pre>
 * <br>----------------------------------------------------------------------
 * <br> <b>功能描述:</b>
 * <br>
 * <br> 注意事项:
 * <br>
 * <br>
 * <br>----------------------------------------------------------------------
 * <br>
 */
public class Browser {

    private static Browser browser;

    public static synchronized Browser getInstance() {
        if (browser == null) {
            browser = new Browser();
        }
        return browser;
    }

    private HttpClient client;

    public Browser() {
        client = new HttpClient();
        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        client.getParams().setParameter(HttpMethodParams.USER_AGENT, "anclient");
        client.getParams().setParameter("http.protocol.single-cookie-header", true);
//        client.getHostConfiguration().setProxy("127.0.0.1", 8888);
//        client.getParams().setAuthenticationPreemptive(true);
    }

    public void setBrowserProperty(String XMI, String cookieStr) {

    }

    public String get(String url, String cookieStr) throws IOException {
        GetMethod method = new GetMethod(url);
        method.setRequestHeader("cookie", cookieStr);
        method.setRequestHeader("X-MI", "AUEEOAeAAAA=");
        method.setRequestHeader("TB-UA", "anclient");

        int status = client.executeMethod(method);
        if (status != 200) {
            throw new RuntimeException("Status:" + status + "\n" + method.getResponseBodyAsString());
        }
        String response = method.getResponseBodyAsString();
        return response;
    }

    public static void main(String[] args) throws IOException {
        Browser browse = Browser.getInstance();
        String cookieStr = "cna=CnJyDK1qd0YCAXOsjOhDVyhd; wud=wud; supportWebp=false; mt=ci=0_0; ck1=; v=0; ALIPAYJSESSIONID=13402a3d5df0c3a715472ddbfaf3f2aa; utsid=Umk3pd5hwYMDACck6F8fHTHb_12278902_1408021128216; imewweoriw=36tnazplXKx57IGl4%2BLnQeArPvxad%2BCzKiuUDXZ4MtU%3D; WAPFDFDTGFG=%2B4cMKKP%2B8PI%2BKY6KYG24iaM7BRXLZI2aNAtC8bioeyUEyQ%3D%3D; _w_tb_nick=ulinchen; ockeqeudmj=o00YPdg%3D; _w_al_f=; uc1=cookie14=UoW3twbDwORttw%3D%3D&cookie21=U%2BGCWk%2F7pY%2FF&cookie15=W5iHLLyFOGW7aA%3D%3D; uc3=nk2=Fma5zxPPFsA%3D&id2=UUtP%2BlQVvPCM&vt3=F8dATSJAgQORfSX9Dts%3D&lg2=UIHiLt3xD8xYTw%3D%3D; skt=1bcacea03a4e1215; lgc=ulinchen; _cc_=URm48syIZQ%3D%3D; t=15f059d07cedcf530bd55b782fc8f3e8; unb=234255417; _nk_=ulinchen; _l_g_=Ug%3D%3D; cookie1=UR5q0VlRnn2yrq2uzXFgPnLoNof5GkxHgV0xbFbyt3g%3D; cookie17=UUtP%2BlQVvPCM; tracknick=ulinchen; sg=n72; cookie2=13402a3d5df0c3a715472ddbfaf3f2aa; _tb_token_=e1ee3393b3e45; _m_h5_tk=9957cab6641e17100e86b35830a20751_1408025482594; _m_h5_tk_enc=7772f4496fd7bdb00f77dc12fba58f87";
        String result = browse.get("http://ax.m.taobao.com/qst.htm?f=w&id=39964511662&uid=234255417&r=1408021166717&cb=", cookieStr);
        System.out.println(result);
    }
}

